package com.aiju.cash.ui.main.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.aiju.cash.R;
import com.aiju.cash.ui.main.entity.Radio;
import com.aiju.cash.ui.main.entity.Tag;
import com.aiju.cash.ui.multiple_shops.widget.MenuLinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：流冥 on 2017/6/16 10:34
 * 说明：
 */

public class BlotterFilterGroupView extends LinearLayout {
    private Context mContext;
    private int column = 3;//默认3列
    private boolean clearTag = false;

    List<RadioGroup> viewList = new ArrayList<>();
    List<RadioButton> radioButtons = new ArrayList<>();

    public BlotterFilterGroupView(Context context) {
        super(context);
        initView(context);
    }

    public BlotterFilterGroupView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public BlotterFilterGroupView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    void initView(Context c) {
        mContext = c;
        setOrientation(VERTICAL);
        setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        setPadding(MenuLinearLayout.dip2px(mContext, 11), MenuLinearLayout.dip2px(mContext, 20), MenuLinearLayout.dip2px(mContext, 11), MenuLinearLayout.dip2px(mContext, 10));
        setBackgroundResource(R.color.transparent);
    }


    public void setData(String title, List<Radio> list) {
        TextView textView = new TextView(mContext);
        LayoutParams l = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        l.setMargins(MenuLinearLayout.dip2px(mContext, 4), 0, 0, 0);
        textView.setLayoutParams(l);

        textView.setTextColor(mContext.getResources().getColor(R.color.color_333333));
        textView.setText(title);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        TextPaint tp = textView.getPaint();
        tp.setFakeBoldText(true);
        addView(textView);
        int size = list.size();
        int tRow = size / column;//一共几行-1
        int tFinalColumn = size % column;//最后一行的列数
        for (int i = 0; i < tRow; i++) {
            LinearLayout linearLayout = new LinearLayout(mContext);
            linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, MenuLinearLayout.dip2px(mContext, 40)));
            linearLayout.setGravity(Gravity.BOTTOM);
            final RadioGroup radioGroup = new RadioGroup(mContext);
            radioGroup.setTag(new Tag(i));
            radioGroup.setClickable(true);
            viewList.add(radioGroup);
            radioGroup.setOrientation(HORIZONTAL);
            radioGroup.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, MenuLinearLayout.dip2px(mContext, 30)));
            for (int j = 0; j < column; j++) {
                int position = i * column + j;
                RadioButton radioButton = new RadioButton(mContext);
                RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(MenuLinearLayout.dip2px(mContext, 104), RadioGroup.LayoutParams.MATCH_PARENT);
                layoutParams.setMargins(MenuLinearLayout.dip2px(mContext, 4), 0, MenuLinearLayout.dip2px(mContext, 4), 0);
                radioButton.setLayoutParams(layoutParams);
                radioButton.setGravity(Gravity.CENTER);
                radioButton.setButtonDrawable(null);
                radioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                radioButton.setBackgroundResource(R.drawable.radiobutton_background);
                radioButton.setText(list.get(position).getName());
                ColorStateList colorStateList = mContext.getResources().getColorStateList(R.color.radiobutton_textcolor);
                radioButton.setTextColor(colorStateList);
                radioGroup.addView(radioButton);
                radioButtons.add(radioButton);
            }
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                    Log.i("radioGroup", "checked1");
                    if (!clearTag) {
                        clearOtherCheck(group);
                        RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                        if (mListener != null)
                            mListener.checkedText(radioButton.getText().toString());
                    } else {
                    }
                }
            });
            linearLayout.addView(radioGroup);
            addView(linearLayout);
        }
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, MenuLinearLayout.dip2px(mContext, 40)));
        linearLayout.setGravity(Gravity.BOTTOM);
        final RadioGroup radioGroup = new RadioGroup(mContext);
        radioGroup.setOrientation(HORIZONTAL);
        radioGroup.setTag(new Tag(tRow));
        radioGroup.setClickable(true);
        radioGroup.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, MenuLinearLayout.dip2px(mContext, 30)));
        for (int j = 0; j < tFinalColumn; j++) {
            int position = (tRow * column) + j;
            RadioButton radioButton = new RadioButton(mContext);
            viewList.add(radioGroup);
            RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(MenuLinearLayout.dip2px(mContext, 104), MenuLinearLayout.dip2px(mContext, 30));
            layoutParams.setMargins(MenuLinearLayout.dip2px(mContext, 4), 0, MenuLinearLayout.dip2px(mContext, 4), 0);
            radioButton.setLayoutParams(layoutParams);
            radioButton.setButtonDrawable(null);
            radioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            radioButton.setGravity(Gravity.CENTER);
            radioButton.setBackgroundResource(R.drawable.radiobutton_background);//设置按钮选中/未选中的背景
            radioButton.setText(list.get(position).getName());
            radioButton.setTextColor(mContext.getResources().getColor(R.color.radiobutton_textcolor));
            radioGroup.addView(radioButton);
            radioButtons.add(radioButton);
        }
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                Log.i("radioGroup", "checked2");
                if (!clearTag) {
                    clearOtherCheck(group);
                    RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                    if (mListener != null)
                        mListener.checkedText(radioButton.getText().toString());
                } else {
                }
            }
        });
        linearLayout.addView(radioGroup);
        addView(linearLayout);

    }

    /**
     * 设置列数
     */
    public void setColumn(int c) {
        column = c;
    }

    public interface RadioGroupListener {
        void checkedText(String value);
    }

    private RadioGroupListener mListener;

    public void setListener(RadioGroupListener listener) {
        mListener = listener;
    }


    private void clearOtherCheck(RadioGroup radioGroup) {
        for (int i = 0; i < viewList.size(); i++) {
            RadioGroup r = viewList.get(i);
            Tag tag = (Tag) r.getTag();
            Tag tag1 = (Tag) radioGroup.getTag();
            clearTag = true;
            if (tag.getId() == tag1.getId()) {
            } else {
                r.clearCheck();
            }
            clearTag = false;
        }
    }

    public void showView(String text) {
        clearTag = true;
        for (int i = 0; i < radioButtons.size(); i++) {
            RadioButton radioButton = radioButtons.get(i);
            String trim = radioButton.getText().toString().trim();
            if (text.equals(trim)) {
                radioButton.setChecked(true);
                break;
            }
        }
        clearTag = false;
    }

}
